package cn.net.msg.cxf.webservice.impl;

import java.io.IOException;
import java.util.Date;

import javax.annotation.Resource;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

import cn.net.msg.cxf.webservice.INewsWebService;
import cn.net.msg.model.NewsDetail;
import cn.net.msg.model.TbFavourite;
import cn.net.msg.model.TbNewslistUrl;
import cn.net.msg.service.TbNewsService;
import cn.net.msg.utils.FilePathUtils;

/**
 * News的CXF接口实现类
 * @author 80419625
 *
 */
@WebService
@SOAPBinding(style = Style.RPC)
@SuppressWarnings("deprecation")
public class NewsWebServiceImpl implements INewsWebService {

    @Resource(name = "tbNewsService")
    private TbNewsService service;

    @Override
    public String getNewsListUrl(int newstype, int currentpage) {
        TbNewslistUrl tbNewslistUrl = service.getNewsListUrl(newstype, currentpage);
        String newsListUrl = "";
        if (tbNewslistUrl != null) {
            newsListUrl = tbNewslistUrl.getNewsListUrl();
        }
        return newsListUrl;
    }

    @Override
    public int addFavouriteNews(long uid, String newsDetailJson) {
        int code = 0;
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            NewsDetail newsDetail = objectMapper.readValue(newsDetailJson, NewsDetail.class);
            TbFavourite tbFavourite = new TbFavourite(uid, newsDetail.getNewsType(),
                    newsDetail.getNewsDetailsTitle(), newsDetail.getNewsDetailsAuthor(), 
                    newsDetail.getNewsDetailsCreateDate(), newsDetail.getNewsDetailsUrl());
            tbFavourite.setFavouritelistCreatdate(new Date());
            boolean isExist = service.checkIsExistFavouriteRecord(tbFavourite);
            if (!isExist) {
                service.addFavouriteNews(tbFavourite); 
            }            
            code = Integer.parseInt(FilePathUtils.readValue("cxfservice.return.code.true"));           
        } catch (JsonParseException e) {
            e.printStackTrace();
            code = Integer.parseInt(FilePathUtils.readValue("cxfservice.return.code.false"));
        } catch (JsonMappingException e) {
            e.printStackTrace();
            code = Integer.parseInt(FilePathUtils.readValue("cxfservice.return.code.false"));
        } catch (IOException e) {
            e.printStackTrace();
            code = Integer.parseInt(FilePathUtils.readValue("cxfservice.return.code.false"));
        } catch (Exception e) {
            e.printStackTrace();
            code = Integer.parseInt(FilePathUtils.readValue("cxfservice.return.code.false"));
        }
        return code;
    }
}
